package cn.getech.data.metadata.controller.module;

import cn.getech.data.intelligence.common.utils.*;
import cn.getech.data.intelligence.common.validator.ValidatorUtils;
import cn.getech.data.intelligence.common.validator.group.AddGroup;
import cn.getech.data.intelligence.common.validator.group.UpdateGroup;
import cn.getech.data.metadata.entity.CollectionSystemModuleEntity;
import cn.getech.data.metadata.model.req.collectionsystemmodule.*;
import cn.getech.data.metadata.model.res.CollectionSystemModuleComboxRes;
import cn.getech.data.metadata.model.res.CollectionSystemModuleRes;
import cn.getech.data.metadata.model.res.owner.Owner;
import cn.getech.data.metadata.service.CollectionSystemModuleService;
import cn.getech.data.metadata.utils.MapUtils;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.getech.system.center.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 系统模块
 *
 * @author qiuxinglei
 * @email qiuxinglei@gmail.com
 * @date 2020-09-29 15:30:41
 */
@Api(value="系统模块",tags={"系统模块"})
@RestController
@RequestMapping("md/collectionsystemmodule")
public class CollectionSystemModuleController {
    @Autowired
    private CollectionSystemModuleService collectionSystemModuleService;

    @ApiOperation("模块列表")
    @PostMapping("/list")
    public PageRes<PageUtils2<CollectionSystemModuleRes>> list(@RequestBody CollectionSystemModuleListReq req){
        ValidatorUtils.validateEntity(req,AddGroup.class);
        Map<String, Object> params = MapUtils.objectToMap(req);
        if(req.getPage() == null ){
            params.put("page",1);
        }
        if(req.getLimit() == null){
            params.put("limit",999);
        }
        PageUtils2<CollectionSystemModuleRes> list = collectionSystemModuleService.queryPage(params);
        if(null == list){
            return BaseRes.okWithPage(new PageUtils2(new ArrayList<CollectionSystemModuleRes>(),0,Integer.parseInt(params.get("limit").toString()),Integer.parseInt(params.get("page").toString())));
        }
        return BaseRes.okWithPage(list);
    }

    @ApiOperation("系统列表")
    @PostMapping("/listSystem")
    @Deprecated
    public DataRes<List<CollectionSystemModuleComboxRes>> listSystem(){
        List<CollectionSystemModuleComboxRes> list = collectionSystemModuleService.listSystem();
        return BaseRes.okWithData(list);
    }

    @ApiOperation("元数据所属")
    @GetMapping("/list/tree") // md/collectionsystemmodule/list/tree
    public R listTree() {
        List<Owner> data = collectionSystemModuleService.listTree();
        return R.okWithData(data);
    }
    @ApiOperation("模块列表")
    @PostMapping("/listModule")
    @Deprecated
    public DataRes<List<CollectionSystemModuleComboxRes>> listModule(@RequestBody CollectionSystemModuleListModuleReq req){
        ValidatorUtils.validateEntity(req, AddGroup.class);
        List<CollectionSystemModuleComboxRes> list = collectionSystemModuleService.listModule(req);
        return BaseRes.okWithData(list);
    }


    @ApiOperation("信息")
    @GetMapping("/info/{smId}")
    @SysLog(value="系统详情",type= OperationTypeEnum.GETCODE,
            modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_GATHER_MODULE_CODE)
    public DataRes<CollectionSystemModuleEntity> info(@PathVariable("smId") Long smId){
        CollectionSystemModuleEntity collectionSystemModule = collectionSystemModuleService.getById(smId);

        return BaseRes.okWithData(collectionSystemModule);
    }

    @ApiOperation("添加元数据所属")
    @PostMapping("/save")
    @SysLog(value="添加元数据所属",type= OperationTypeEnum.ADDCODE, modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_SYSTEM_MANAGER_MODULE_CODE)
    public BaseRes save(@RequestBody CollectionSystemModuleAddReq req){
        ValidatorUtils.validateEntity(req, AddGroup.class);
        collectionSystemModuleService.save(req, ShiroUtils.getUserId());

        return BaseRes.ok();
    }

    @ApiOperation("修改元数据所属")
    @PostMapping("/update")
    @SysLog(value="修改元数据所属",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_SYSTEM_MANAGER_MODULE_CODE)
    public BaseRes update(@RequestBody CollectionSystemModuleUpdateReq req){
        ValidatorUtils.validateEntity(req, UpdateGroup.class);
        collectionSystemModuleService.update(req, ShiroUtils.getUserId());
        
        return BaseRes.ok();
    }

    @ApiOperation("删除元数据所属")
    @PostMapping("/delete")
    @SysLog(value="删除元数据所属",type= OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_SYSTEM_MANAGER_MODULE_CODE)
    public BaseRes delete(@RequestBody CollectionSystemModuleDeleteReq req){
        ValidatorUtils.validateEntity(req, AddGroup.class);
        collectionSystemModuleService.delete(req);
        return BaseRes.ok();
    }

}
